package com.baizhi.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Video;
import com.baizhi.service.VideoService;
import com.baizhi.vo.CommonResult;
import com.baizhi.vo.VideoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@RestController  //将controller组件对象交给工厂管理
@CrossOrigin  //跨域： 当一个请求url的协议、域名、端口三者之间任意一个与当前页面url不同即为跨域
@RequestMapping("video")
public class VideoController {

    private static final Logger log = LoggerFactory.getLogger(VideoController.class);
    @Resource
    private VideoService videoService;

    @RequestMapping("queryAllPage")
    @ResponseBody
    //分页查询视频
    private VideoVO queryAllPage(@RequestBody PageDTO pageDTO){
        return videoService.queryAllPage(pageDTO);
    }

    //上传视频
    @RequestMapping("uploadVideo")
    @ResponseBody
    public String uploadHeadImg(MultipartFile videoFile) throws IOException {
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAI5tMubzuDJQH7g49REfo2";
        String accessKeySecret = "yJCTbvX9pU22rErROBuh87h1mrfgQQ";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);

        // 填写Byte数组。
        byte[] content = videoFile.getBytes();
        //解决文件重名
        String filename = new Date().getTime()+"-"+videoFile.getOriginalFilename();
        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject("yingxue-images", "video/"+filename, new ByteArrayInputStream(content));

        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://yingxue-images.oss-cn-beijing.aliyuncs.com/video/"+filename;
    }

    //添加视频
    @RequestMapping("add")
    @ResponseBody
    public Map<String, String> add(@RequestBody Video video){

        video.setCreateTime(new Date());
        video.setStatus(1);
        return videoService.add(video);
    }

    //删除视频
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, String> delete(@RequestBody Video video){
        return videoService.delete(video.getId());
    }

    //根据id查询
    @RequestMapping("queryById")
    @ResponseBody
    public Video queryById(int id){
        return videoService.queryById(id);
    }


    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(@RequestBody Video video) {
        log.info("videos：{}", video);
        try {
            videoService.update(video);
            return CommonResult.success("修改成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.faild("修改失败！！！");
        }
    }


}
